<template>
	<!-- 搜索页面 -->
	<view class="search_container">
		<view class="search_box">
			<uni-search-bar 
				class="uni-mt-10" 
				radius="5" 
				placeholder="输入商品名称" 
				clearButton="auto" 
				cancelButton="true"
				@confirm="searchKeyUpHandle"
				v-model="searchValue"
				@input="searchChange"
				:focus="true"
				/>
		</view>
		<!-- 搜索历史 -->
		<view class="search_history" v-show="!tipsBoxFlag">
			<view class="search_history_title">
				<view class="search_history_title_text">
					历史搜索
				</view>
				<view class="clear_his_btn" v-if="!histEmptyFlag" @click="deleteHistoryHandle">
					<image style="width: 20px;" src="../../static/删除.png" mode="widthFix"></image>
				</view>
			</view>
			<view class="search_history_items">
				<view class="empty_his" v-if="histEmptyFlag">
					搜索为空
				</view>
				<view class="search_history_itemss">
					<view 
						class="search_history_item"  
						v-for="(item, index) of hisSearchD" 
						:key="index"
						@click="selectHistoryHandle(item)"
						>
						{{ item }}
					</view>
				</view>
			</view>
		</view>
		<!-- 输入提示 -->
		<view class="tips_box">
			<view class="tip_item" 
				v-for="(item, index) of tipsValue" 
				:key="index"
				@click="gotoGoodsDetail(item)"
				>
				<view class="tip_search_icon">
					<image class="tip_search_icon_img" src="../../static/search.png" mode="widthFix"></image>
				</view>
				<view class="tip_item_text">
					{{ item.goodsName }}
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { onShow } from "@dcloudio/uni-app"

// 搜索值
let searchValue = ref("");
// 提示词列表
let tipsValue = ref([]);
// 搜索提示词框显示控制
let tipsBoxFlag = ref(false);
// 历史搜索列表
let hisSearchD = ref([]);
// 历史记录为空标识
let histEmptyFlag = ref(true);

onMounted(()=>{
	getHistorySearch();
})

onShow(()=>{
	getHistorySearch();
})

// 获取历史搜索
function getHistorySearch(){
	// 判断是否有历史记录
	if(uni.getStorageSync("search_history") == ""){
		hisSearchD.value = [];
		histEmptyFlag.value = true;
		return 1;
	}
	
	let historyArr = JSON.parse(uni.getStorageSync("search_history"));
	
	if(historyArr.length == 0){
		hisSearchD.value = [];
		histEmptyFlag.value = true;
		return 1;
	}
	
	histEmptyFlag.value = false;
	hisSearchD.value = historyArr;
}

// 搜索处理器
function searchKeyUpHandle(){
	// 将搜索文本加入历史搜索列表
	if(uni.getStorageSync("search_history") == ""){
		// 初始化历史列表
		let initArr = [searchValue.value];
		uni.setStorageSync("search_history", JSON.stringify(initArr));
	}else{
		let historyArr = JSON.parse(uni.getStorageSync("search_history"));
		// 检查，防止重复
		let repFlag = false;
		for(let text of historyArr){
			if(text == searchValue.value){
				repFlag = true;
				break;
			}
		}
		
		// 非重复，添加
		if(!repFlag){
			historyArr.push(searchValue.value);
			uni.setStorageSync("search_history", JSON.stringify(historyArr));
		}
	}
	
	// 跳转到商品列表页面
	gotoGoodsListPage();
}

// 跳转到商品列表页面
function gotoGoodsListPage(){
	uni.navigateTo({
		url:`/subPkg1/goods_list/goods_list?navText=${searchValue.value}&type=search`
	})
}

// 点击历史搜索
function selectHistoryHandle(touchText){
	uni.navigateTo({
		url:`/subPkg1/goods_list/goods_list?navText=${touchText}&type=search`
	})
}

// 输入框变化
async function searchChange(value){
	const res = await uni.$http.get(`/goodsInfos/getGoodsInfosByName/${value}`);
	tipsValue.value = res.data.data;
	
	if(res.data.data == null || res.data.data.length == 0){
		// 隐藏搜索提示词框
		tipsBoxFlag.value = false;
	}else{
		// 显示提示词框
		tipsBoxFlag.value = true;
	}
}

// 点击提示词商品，进入到商品详情
async function gotoGoodsDetail(item){
	// 根据商品ID搜索商品信息，并存入本地和跳转
	const res = await uni.$http.get(`/goodsInfos/getGoodsInfosById/${item.goodsId}`);
	
	uni.setStorageSync("goodsDetail", JSON.stringify(res.data.data));
	uni.navigateTo({
		url:"/subPkg1/goods_detail/goods_detail"
	})
}

// 删除历史搜索
function deleteHistoryHandle(){
	uni.setStorageSync("search_history", JSON.stringify([]));
	getHistorySearch();
}
</script>

<style lang="scss" scoped> 
.search_box{
	background-color: #fff;
}

.tips_box{
	background-color: #fff;
	padding: 0px 20rpx;
	font-size: 13px;
	color: #444444;
}

.tip_item{
	position: relative;
	padding: 18rpx 0px;
	border-bottom: #f1f1f1 1px solid;
	display: flex;
	align-items: center;
}

.tip_search_icon_img{
	width: 20px;
}

.tip_item_text{
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	padding-left: 8px;
}

.search_history{
	background-color: #fff;
	padding: 20rpx 20px;
}

.search_history_title_text{
	font-weight: 600;
}

.clear_his_btn{
	font-size: 13px;
	
}

.search_history_title{
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.empty_his{
	font-size: 14px;
	margin-top: 20rpx;
	color: #414141;

}

.search_history_itemss{
	display: flex;
	flex-wrap: wrap;
	position: relative;
	right: 10px;
	padding-bottom: 20rpx;
}

.search_history_item{
	background-color: #e1e1e1;
	padding: 4px;
	font-size: 13px;
	margin-top: 10px;
	margin-left: 10px;
	border-radius: 10rpx;
}

</style>
